package web.controller;

import hibernate.pojo.Voting;
import hibernate.util.HibernateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.faces.component.UIData;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class ListAllVotingsController {


	private static final Log logger = LogFactory.getLog(ListAllVotingsController.class);

	private List<Voting> votings;
	private Voting voting;
	private UIData votingsTable;
	private String votingID;
	private String votingName;
	private String startDate;
	private String endDate;
	private String detailInfo;
	
	private static final String[] WEEK_DAY_LABELS = new String[] { "Sun *",
		"Mon +", "Tue +", "Wed +", "Thu +", "Fri +", "Sat *" };
	
	private Date selectedStartDate;
	private Date selectedEndDate;
	private boolean popup;
	private Locale locale;
	private String pattern;
	private boolean useCustomDayLabels;
	private boolean showApplyButton;

	public ListAllVotingsController() {
		// TODO Auto-generated constructor stub
		locale = Locale.US;
		popup = true;
		pattern = "dd/MM/yyyy";
	}

	public String goruntule(){
		String pageToReturn = "";
		try {

			setVoting(((Voting)getVotingsTable().getRowData()));
			logger.info("Initial Voting Values!");
			logger.info(getVoting().getVotingName());
			logger.info(getVoting().getStartDate());
			logger.info(getVoting().getEndDate());
			logger.info(getVoting().getDetailInfo());
			logger.info(getVoting().getNumberOfSelections());
			logger.info(getVoting().getVotingID());
			pageToReturn = "showVotingDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen telefonu getirirken hata olustu!", exception);
		}
		return pageToReturn;
	}

	public String updateVoting(){
		String pageToRedirect = "";

		try {

			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(voting);
			transaction.commit();

			pageToRedirect = "updateVotingInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		

		return pageToRedirect;
	}

	public String displayAsPieChart(){
		String pageToRedirect = "";

		try {
			//export to Excel function will be added here
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(voting);
			transaction.commit();

			pageToRedirect = "updateVotingInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		

		return pageToRedirect;
	}
	
	public String deleteVotingRecord(){
		String pageToRedirect = "";

		try {
			//export to Excel function will be added here
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.delete(voting);
			transaction.commit();

			pageToRedirect = "updateVotingInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		

		return pageToRedirect;
	}
	
	public String cancelUpdateVotingRecord() {
		
		String pageToRedirect ="displayListOfAllVotings";
		
		try {
			
		} catch (Exception exception) {
			logger.error("Error at Cancel Insert New Voting Record", exception);
		}
		
		return pageToRedirect;
		
	}
	
	public String listVotingOptions() {
		String pageToRedirect = "";
		
		try {
		
			
			setVoting(((Voting)getVotingsTable().getRowData()));
			
			
			
			pageToRedirect = "listVotingOptions";
		} catch (Exception exception) {
			logger.error("Error when showing voting options!", exception);
		}
		
		return pageToRedirect;
		
	}
	
	public List<Voting> getVotings() {
		try {
			votings = new ArrayList<Voting>();
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Voting.class);
			votings = criteria.list();
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}

		if(votings != null && votings.size() > 0){
			return votings;
		} else 
			return null;
	}

	public void setVotings(List<Voting> votings) {
		this.votings = votings;
	}

	public Voting getVoting() {
		return voting;
	}

	public void setVoting(Voting voting) {
		this.voting = voting;
	}

	public UIData getVotingsTable() {
		return votingsTable;
	}

	public void setVotingsTable(UIData votingsTable) {
		this.votingsTable = votingsTable;
	}

	public String getVotingID() {
		return votingID;
	}

	public void setVotingID(String votingID) {
		this.votingID = votingID;
	}

	public String getVotingName() {
		return votingName;
	}

	public void setVotingName(String votingName) {
		this.votingName = votingName;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getDetailInfo() {
		return detailInfo;
	}

	public void setDetailInfo(String detailInfo) {
		this.detailInfo = detailInfo;
	}

	public Date getSelectedStartDate() {
		if(getVoting().getStartDate() != null)
			return getVoting().getStartDate();
		return selectedStartDate;
	}

	public void setSelectedStartDate(Date selectedStartDate) {
		this.selectedStartDate = selectedStartDate;
	}

	public Date getSelectedEndDate() {
		if(getVoting().getEndDate() != null)
			return getVoting().getEndDate();
		return selectedEndDate;
	}

	public void setSelectedEndDate(Date selectedEndDate) {
		this.selectedEndDate = selectedEndDate;
	}

	public boolean isPopup() {
		return popup;
	}

	public void setPopup(boolean popup) {
		this.popup = popup;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	
	public Object getWeekDayLabelsShort() {
		if (isUseCustomDayLabels()) {
			return WEEK_DAY_LABELS;
		} else {
			return null;
		}
	}

	public boolean isUseCustomDayLabels() {
		return useCustomDayLabels;
	}

	public void setUseCustomDayLabels(boolean useCustomDayLabels) {
		this.useCustomDayLabels = useCustomDayLabels;
	}

	public boolean isShowApplyButton() {
		return showApplyButton;
	}

	public void setShowApplyButton(boolean showApplyButton) {
		this.showApplyButton = showApplyButton;
	}


}
